<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Shortlist extends Model
{
    protected $table = 'short_list';
    protected $primaryKey = 'id';

    public static function getUserShortList($openid,$name,$group_id)
    {
        $query = self::where("openid",$openid)->where("status","<",3);
        if (!empty($name)) $query->where("name","like","%".$name."%");
            if (!empty($group_id)) $query->where("group_id",$group_id);
        return $query->orderBy("id","DESC")->simplePaginate(10);
    }

    public static function allShortList()
    {
        $query = self::where("status","<",3);
        return $query->orderBy("id","DESC")->simplePaginate(10);
    }

    public static function getUserShortCount($openid)
    {
        return self::where("openid",$openid)->count("id");
    }

    public static function updateShortId($id,$short_id,$short_link)
    {
        return self::where("id",$id)->update(["short_id"=>$short_id,"short_link"=>$short_link]);
    }

    public static function shortListDetial($short_id)
    {
        return self::select("link","end_time","deadline","domain","status")->where("short_id",$short_id)->where("status",1)->first();
    }

    public static function userShortList($openid)
    {
        return self::select("short_id")->where("openid",$openid)->where("status",1)->get();
    }

    public static function shortListDel($openid,$short_id)
    {
        return self::where("openid",$openid)->where("short_id",$short_id)->update(["status"=>3]);
    }

    public static function shortDetial($openid,$short_id)
    {
        return self::where("openid",$openid)->where("short_id",$short_id)->first();
    }

    public static function shortLinkUpdate($openid,$short_id,$name,$link,$end_time,$group_id,$deadline,$domain,$short_link)
    {
        return self::where("openid",$openid)->where("short_id",$short_id)->update(["name"=>$name,"link"=>$link,"end_time"=>$end_time,"group_id"=>$group_id,"deadline"=>$deadline,"domain"=>$domain,"short_link"=>$short_link]);
    }

    public static function shortLinkCreate($openid,$name,$link,$end_time,$group_id,$deadline,$domain,$appid)
    {
        return self::insertGetId(["openid"=>$openid,"name"=>$name,"link"=>$link,"end_time"=>$end_time,"group_id"=>$group_id,"deadline"=>$deadline,"domain"=>$domain,"appid"=>$appid,"created_at"=>time()]);
    }

    public static function shortLinkDeadline($openid,$deadline)
    {
        return self::where("openid",$openid)->update(["deadline"=>$deadline]);
    }

    public static function userShortListDel($openid)
    {
        return self::where("openid",$openid)->update(["status"=>3]);
    }
}